import java.io.FileReader;
import org.omg.CORBA.*;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;


/**
 *
 * @author  daniele
 */
public class Log_main extends javax.swing.JFrame {

   // private Log_viewerImpl log_viewerImpl;

   // private ORB orb;

    static String args[];
    private static final String IORFILE = "/home/daniele/SCD/f1-export/txt/ior.txt";

    private void startORB(){
         
    }


    /** Creates new form Log_Frame */
    public Log_main() {
        initComponents();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("F1Sim SCD");
        addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                formMouseWheelMoved(evt);
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

       // jTable.setModel(new DriversTableModel(new Vector<Driver>()));
        jScrollPane1.setViewportView(jTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 621, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_formMouseWheelMoved
        // TODO add your handling code here:
    }//GEN-LAST:event_formMouseWheelMoved

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        //orb.shutdown(false);
    }//GEN-LAST:event_formWindowClosing

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Log_main().setVisible(true);
            }
        });

        try{
          // create and initialize the ORB
          ORB orb = ORB.init(args,null);

          // get reference to rootpoa & activate the POAManager
          POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
          rootpoa.the_POAManager().activate();

          // create servant
          Log_viewerImpl log_viewerImpl = new Log_viewerImpl();
          log_viewerImpl.setORB(orb);

          // get object reference from the servant
          org.omg.CORBA.Object ref = rootpoa.servant_to_reference(log_viewerImpl);
          Log_viewer href = Log_viewerHelper.narrow(ref);
          
          //get NameService
          FileReader fileIn = new FileReader(IORFILE);
          String ior = "";
          while(fileIn.ready()){
              char c = (char)fileIn.read();
              if(c!= '\n'){
                 ior = ior + c;
              }
          }
          fileIn.close();
          System.out.println("NameService IOR readed from file: "+IORFILE);
          System.out.println("NameService: -"+ior+"-");


        
          //If resolve_initial_references don't work, need to use ior readed from file
          //resolve_initial_references("NameService");
          org.omg.CORBA.Object ncobj = orb.string_to_object(ior);
          NamingContext rootNC = NamingContextHelper.narrow(ncobj);

          NameComponent[] name = new NameComponent[1];
          name[0] = new NameComponent("Logger","");

          rootNC.bind(name, ref);

          System.out.println("Logger ready and waiting ...");


          // wait for invocations from client
          orb.run();
      }

      catch (Exception e) {
        System.err.println("ERROR: " + e);

        e.printStackTrace(System.out);
      }

      System.out.println("Logger Exiting ...");

       
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable;
    // End of variables declaration//GEN-END:variables


}
